package dkbta.util.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Holds various useful methods for parsing and formatting dates and working with time
 * units
 * 
 * @author Uri Kanonov
 */
public class TimeUtils{
	private static final DateFormat _timeOnly = new SimpleDateFormat("HH:mm:ss");

	private static final DateFormat _timeAndDate = new SimpleDateFormat(
			"HH:mm:ss dd/MM/yyyy");

	private static final DateFormat _timeInclMillisAndDate = new SimpleDateFormat(
			"dd/MM/yyyy HH:mm:ss.SSS");

	/**
	 * Parses the time in milliseconds from a string representing a date according to the
	 * "dd/MM/yyyy HH:mm:ss" format
	 * 
	 * @param date The string
	 * @return The parsed time in milliseconds or null if the string couldn't be parsed
	 */
	public static Long parseDate(String date){
		try{
			return _timeAndDate.parse(date).getTime();
		}catch(ParseException e){
			return null;
		}
	}

	/**
	 * Parses the number of milliseconds from a string
	 * 
	 * @param millis The string
	 * @return The parsed number of milliseconds or null if the string couldn't be parsed
	 */
	public static Long parseMillis(String millis){
		try{
			return Long.valueOf(millis);
		}catch(NumberFormatException e){
			return null;
		}
	}

	/**
	 * Produces a string representing a given time according to the dd/MM/yyyy HH:mm:ss
	 * format
	 * 
	 * @param date The time in milliseconds
	 * @return The formatted string
	 */
	public static String timeAndDate(long date){
		return _timeAndDate.format(date);
	}

	/**
	 * Produces a string representing a given time according to the dd/MM/yyyy
	 * HH:mm:ss.SSS format
	 * 
	 * @param date The time in milliseconds
	 * @return The formatted string
	 */
	public static String timeInclMillisAndDate(long date){
		return _timeInclMillisAndDate.format(date);
	}

	/**
	 * Produces a string representing a given time according to the HH:mm:ss format
	 * 
	 * @param date The time in milliseconds
	 * @return The formatted string
	 */
	public static String timeOnly(long date){
		return _timeOnly.format(date);
	}

	/**
	 * Converts a rate that is per the given <tt>timeUnit</tt> to be per millisecond.
	 * <br>
	 * For example if given a value of 5000 and it is per second, then the returned value
	 * is 5 (per millisecond)
	 * 
	 * @param valueInTu The value to be converted to be per millisecond
	 * @param timeUnit The time unit per which the initial value is
	 * @return The converted value per millisecond
	 */
	public static double convertFromTimeUnitToMillis(double valueInTu, TimeUnit timeUnit){
		return valueInTu / timeUnit.inMillis;
	}

	/**
	 * Returns how many milliseconds there are in the given <tt>timeInTu</tt>
	 * 
	 * @param timeUnit The time unit in which the time is given
	 * @param timeInTu Time in the units of the given time unit
	 * @return The time in milliseconds
	 */
	public static long convertTimeUnitToMillis(TimeUnit timeUnit, long timeInTu){
		return timeInTu * timeUnit.inMillis;
	}

	/**
	 * Returns how many times does <tt>timeUnit</tt> fit into the given millis
	 * 
	 * @param millis The amount of time in millis
	 * @param timeUnit The time unit
	 * @return How many times does <tt>timeUnit</tt> fit into the given millis
	 */
	public static double convertMillisToTimeUnit(long millis, TimeUnit timeUnit){
		return millis / timeUnit.inMillis;
	}
}
